/**********************************************************************
gc_10_structural_fit_basic.do

**********************************************************************/
**********
* SET UP *
**********
clear all
set matsize 2000
set more 1

* location for dofiles *
cd "T:\_Projet_4915\dofiles"
global dir "`c(pwd)'"
cd $dir

set seed 8397740

sysdir set PERSONAL "T:\_Projet_4915\ado"

* specify file locations *
global project_folder "\_Projet_4915"
global data_folder "\_Projet_4915\DATA"
global output_folder "\_Projet_4915\ResultsFolder"
global temp "temp"

local datadir T:\${data_folder}\

*********
* GATES *
*********
* Specify which data you want to work with (synthetic = syn, real = rl) *
local ext = "rl"

* Specify which gender to process (men = men) *
local gender = "men"

* Specify first observed age (24 25 26) *
local a = 25

* Specify occupational definition to use (occ_version 1 2)
local v = 2

* Specify firm class definition (1 2 3 4 5 6)
local d = 6

* Specify the number of unobservable types *
global types "5"

* Specify earnings data generating process *
global dgps "2"

* STEP 1: create a validation dataset *
local gate1 = 1

* STEP 2: fit *
local gate2 = 1

* STEP X: delete all intermediate datasets *
local gateX = 2

*******************************************
* MAKE A GLOBAL VARIABLE FOR TODAY'S DATE *
*******************************************
local tyr = substr("$S_DATE",8,4)
local tmo = substr("$S_DATE",4,3)
local tmd = trim(substr("$S_DATE",1,2))

* make day of month two digits *
local wl = length("`tmd'")
if `wl'==1 {
local tmd2 ="0"+"`tmd'"
}
if `wl'!=1 {
local tmd2 ="`tmd'"
}

* get numeric month, make it two digits *
local di="`tmd2'"+"`tmo'"+"`tyr'"
local edate = date("`di'", "DMY")
local mono = month(`edate')
local ml = length("`mono'")
if `ml'==1 {
local mono2 ="0"+"`mono'"
}
if `ml'!=1 {
local mono2 ="`mono'"
}

* put final date together *
global date = "`tyr'"+"`mono2'"+"`tmd2'"

****************************************
* DEFINE LITTLE PROGRAMS TO PRINT TIME *
****************************************
program define starttime
	display "Started processing at $S_TIME on $S_DATE"
end

program define endtime
	display "Finished processing at $S_TIME on $S_DATE"
end


************************
* START OF THE PROGRAM *
************************
foreach y_dgp of global dgps ///
{
foreach n_types of global types ///
{
* start log file *
quietly capture log close
quietly log using gc_10_structural_fit_B_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'_`ext', text replace

***************************************
* STEP 1: construct simulated dataset *
***************************************
disp "***** Started processing STEP 1 *****"
starttime
if 1 == `gate1' ///
{
	disp "***** STEP 1: construct simulated dataset *****"
	disp "***** Time is now $S_TIME *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	keep if age == 1
	rename state state_index
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	rename state_index state
	reshape wide q, i(pid_int) j(state)
	keep pid_int q* age Laction exper_10 exper_20 exper_30
	order pid_int q* age Laction exper_10 exper_20 exper_30
	
	**************************************
	* STEP 1.1: DRAW UNOBSERVABLE TYPE *
	**************************************
	if `n_types' == 3 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw <= c
	}

	if `n_types' == 4 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
		gen d = c + q4
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw < c
		replace state = 4 if draw >= c & draw <= d
	}
	
	if `n_types' == 5 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
		gen d = c + q4
		gen e = d + q5
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw < c
		replace state = 4 if draw >= c & draw < d
		replace state = 5 if draw >= d & draw <= e
	}
	
	if `n_types' == 6 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
		gen d = c + q4
		gen e = d + q5
		gen f = e + q6
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw < c
		replace state = 4 if draw >= c & draw < d
		replace state = 5 if draw >= d & draw < e
		replace state = 6 if draw >= e & draw <= f
	}
	
	global keep_vars ""
	foreach var in ///
	"Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var'_index = `var'
		global keep_vars "$keep_vars `var'_index"
	}
	gen Laction_rl = Laction
	global keep_vars "pid_int state q* $keep_vars Laction_rl"
	keep $keep_vars
	order $keep_vars
	
	local big_T = 1
	while `big_T' <= 10 ///
	{
	gen V_0 = 1
	
	***************************
	* STEP 1.2: FIX ACTION j' *
	***************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* construct synthetic aggregate experience variables *
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP10
	gen b = a + CCP20
	gen c = b + CCP30
	gen d = c + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action *
	gen zaction_jp = 0
	replace zaction_jp = 10 if draw < a
	replace zaction_jp = 20 if draw >= a & draw < b
	replace zaction_jp = 30 if draw >= b & draw < c
	replace zaction_jp = 90 if draw >= c & draw <= d
	
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	****************************
	* STEP 1.3: FIX ACTION j'' *
	****************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_jp
	replace age = age + 1

	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
	}
	
	* construct synthetic aggregate experience variables *
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP10
	gen b = a + CCP20
	gen c = b + CCP30
	gen d = c + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action j'' *
	gen zaction_jpp = 0
	replace zaction_jpp = 10 if draw < a
	replace zaction_jpp = 20 if draw >= a & draw < b
	replace zaction_jpp = 30 if draw >= b & draw < c
	replace zaction_jpp = 90 if draw >= c & draw <= d
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	***************************************************
	* STEP 1.4: FOR EACH j in J CALCULATE OFFSET TERM *
	***************************************************
	foreach j in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Pick action j *
		gen zaction_j = `j'
		* Pick action cj *
		gen zaction_cj = zaction_jp
		* Pick action cjp *
		gen zaction_cjp = zaction_j
		* Pick action cjpp *
		gen zaction_cjpp = zaction_jpp
		
		****************************
		* CCPs and income associated with j *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_10" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
	
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_j = 0
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace zln_y_hat_j = ln_y_hat_`act' if zaction_j == `act'
		}
		replace zln_y_hat_j = ln(10400) if zaction_j == 90
		drop ln_y_hat*
	
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************
		* CCPs and income associated with j'. *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_10" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_j
		replace age = age + 1
	
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"10" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"10" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jp = .
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace zCCP_jp = CCP`act' if zaction_jp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jp = 0
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace zln_y_hat_jp = ln_y_hat_`act' if zaction_jp == `act'
		}
		replace zln_y_hat_jp = ln(10400) if zaction_jp == 90
		drop ln_y_hat*
		
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with j''. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_10" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_jp
		replace age = age + 2
	
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"10" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"10" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP10+ CCP20 + CCP30 + CCP90
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jpp = .
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace zCCP_jpp = CCP`act' if zaction_jpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jpp = 0
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace zln_y_hat_jpp = ln_y_hat_`act' if zaction_jpp == `act'
		}
		replace zln_y_hat_jpp = ln(10400) if zaction_jpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
	
		***************************************
		* CCPs and income associated with cj. *
		***************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_10" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cj = 0
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace zln_y_hat_cj = ln_y_hat_`act' if zaction_cj == `act'
		}
		replace zln_y_hat_cj = ln(10400) if zaction_cj == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with cj'. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_10" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cj
		replace age = age + 1
		
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}		
		
		local counter = 1
		
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"10" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"10" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
	
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjp = .
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjp = CCP`act' if zaction_cjp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjp = 0
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjp = ln_y_hat_`act' if zaction_cjp == `act'
		}
		replace zln_y_hat_cjp = ln(10400) if zaction_cjp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		******************************
		* CCPs associated with cj''. *
		******************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_10" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cjp
		replace age = age + 2
	
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_cjp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"10" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"10" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjpp = .
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjpp = CCP`act' if zaction_cjpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjpp = 0
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjpp = ln_y_hat_`act' if zaction_cjpp == `act'
		}
		replace zln_y_hat_cjpp = ln(10400) if zaction_cjpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		local beta = .90
		display "`beta'"
		
		*************************
		* Calculate offset term *
		*************************
		gen Offset = `beta'*log(zCCP_jp/zCCP_cjp) + `beta'*`beta'*log(zCCP_jpp/zCCP_cjpp)
		
		********************************************************************
		* Calculate Delta U using estimated parameters of utility function *
		********************************************************************
		gen z_lny = zln_y_hat_j-zln_y_hat_cj + `beta'*(zln_y_hat_jp-zln_y_hat_cjp) + `beta'*`beta'*(zln_y_hat_jpp-zln_y_hat_cjpp)
	
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			gen amen`act'_j = 0
			replace amen`act'_j = 1-`beta' if zaction_j == `act'
			gen amen`act'_jp = 0
			replace amen`act'_jp = -1*(1-`beta') if zaction_jp == `act'
			gen zamen`act' = amen`act'_j + amen`act'_jp
			drop amen*
		}
		
		display "`beta'"
		foreach act in ///
		"10" ///
		"20" "30" ///
		{
			foreach Lact in ///
			"10" ///
			"20" "30" "90" ///
			{
				gen cost_act`Lact'_`act'_z_j = 0
				replace cost_act`Lact'_`act'_z_j = 1 if Laction_index == `Lact' & zaction_j == `act' & Laction_index != zaction_j
				gen cost_act`Lact'_`act'_z_jp = 0
				replace cost_act`Lact'_`act'_z_jp = -1 if Laction_index == `Lact' & zaction_jp == `act' & Laction_index != zaction_jp
				gen cost_act`Lact'_`act'_j_jp = 0
				replace cost_act`Lact'_`act'_j_jp = `beta' if zaction_j == `Lact' & zaction_jp == `act' & zaction_j != zaction_jp
				gen cost_act`Lact'_`act'_jp_j = 0
				replace cost_act`Lact'_`act'_jp_j = -`beta' if zaction_jp == `Lact' & zaction_j == `act' & zaction_jp != zaction_j
				gen cost_act`Lact'_`act'_jp_jpp = 0
				replace cost_act`Lact'_`act'_jp_jpp = `beta'*`beta' if zaction_jp == `Lact' & zaction_jpp == `act' & zaction_jp != zaction_jpp
				gen cost_act`Lact'_`act'_j_jpp = 0
				replace cost_act`Lact'_`act'_j_jpp = -`beta'*`beta' if zaction_j == `Lact' & zaction_jpp == `act' & zaction_j != zaction_jpp
				gen zcost_act`Lact'_`act' = cost_act`Lact'_`act'_z_j+cost_act`Lact'_`act'_z_jp+cost_act`Lact'_`act'_j_jp+cost_act`Lact'_`act'_jp_j+cost_act`Lact'_`act'_jp_jpp + cost_act`Lact'_`act'_j_jpp
				*gen zcost_act`Lact'_`act'_age = zcost_act`Lact'_`act'*age
				drop cost*
			}
		}

		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_util_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict Delta_U
		estimates clear
	
		* Now calculate v_j *
		gen v_`j' = Delta_U - Offset
	
		* Now calculate e_j *
		gen rgumbel_`j' = -log(-log(runiform()))
			
		* Now calculate V_j *
		gen V_`j' = v_`j' + rgumbel_`j'

		keep $keep_vars zaction_jp zaction_jpp V_*
	}
		
	**********************************************************
	* Now choose action that maximizes V_j, call that action *
	**********************************************************
	gen maxV = max(V_10,V_20,V_30,V_90)
	gen action = 0
	foreach act in ///
	"10"  ///
	"20" "30" "90" ///
	{
		replace action = `act' if round(V_`act',.001) == round(maxV,.001)
	}

	* save cross-section before updating state variables *
	preserve
	foreach var in ///
	"Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	keep pid_int state q* age action Laction exper_10 exper_20 exper_30
	order pid_int state q* age action Laction exper_10 exper_20 exper_30
	gen t = `big_T'
	* construct aggregate experience variables *
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Simulate log earnings using estimated payoff functions *
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
		
	* Pick the relevant ln_y_hat *
	gen ln_y_hat = .
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	drop ln_y_hat_*
	
	local datadir T:\${data_folder}\
	disp "`datadir'"
	compress
	save "`datadir'gc_tmpfit_`big_T'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	restore
	
	* Forward simulate variables for next cross section *
	replace Laction_rl = action
	replace Laction_index = action if action != 90
	replace Laction_index = Laction_rl if action == 90
	
	replace age_index = age_index+1
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace exper_`act'_index = exper_`act'_index + 1 if action == `act'
	}
	
	keep $keep_vars
	order $keep_vars
	local big_T = `big_T'+1
	}
	
	* Calculate variance of idiosyncratic shocks for payoff functions (to be used below to simulate earnings) *
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	foreach var in "state" "age" ///
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		rename `var' `var'_index
	}
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	merge m:1 state_index age_index ///
	exper_10_index ///
	exper_20_index exper_30_index ///
	using "`datadir'gc_structural_ln_y_hats_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	foreach var in "state" "age" ///
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		rename `var'_index `var'
	}
	
	gen ln_y_hat = .
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	sort pid_int tax_yr state
	gen mu = ln_y-ln_y_hat
	sort action
	gen sigma = .
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		sum mu [aw=q] if action == `act'
		replace sigma = `r(sd)' if action == `act'
	}
	keep action sigma
	duplicates drop
	save "`datadir'gc_structural_sigmas_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace

	use "`datadir'gc_tmpfit_1_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	forvalues t = 2(1)10 ///
	{
		append using "`datadir'gc_tmpfit_`t'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	}
	merge m:1 action using "`datadir'gc_structural_sigmas_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	gen shock = rnormal(0,sigma)
	gen ln_y = ln_y_hat+shock
	drop ln_y_hat shock sigma
	compress
	save "`datadir'gc_structural_fit_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	clear
	
	capture erase "`datadir'gc_structural_sigmas_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	forvalues t = 1(1)10 ///
	{
		capture erase "`datadir'gc_tmpfit_`t'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	}

	disp "***** STEP 1: create a validation dataset (COMPLETED) *****"
}
disp "***** Finished processing STEP 1 *****"
endtime

***************
* STEP 2: fit *
***************
disp "***** Started processing STEP 2 *****"
starttime
if 1 == `gate2' ///
{
	disp "***** STEP 2: fit *****"
	
	*********************************
	* Row transition matrix, age 30 *
	*********************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_fit_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	rename t tax_yr
	sort pid_int tax_yr	
	replace age = age+25
	
	keep if age == 30
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	
	*** calculate the share of action by last action ***
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 (count) ind=n_ind obs=pid_int [aw=q] 
		gen Laction = "`Lact'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`Lact'" == "10" ///
    	{
    		save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
    	}
  		if "`Lact'" != "10" ///
    	{
    		append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id 
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 (count) ind=n_ind obs=pid_int [aw=q] 
	gen Laction = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 [aw=q] 
		gen Laction = "`Lact'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 [aw=q] 
	gen Laction = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order Laction
	sort Laction
	save "`datadir'gc_reduced_B_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order Laction
	sort Laction
	forvalues j = 1(1)4 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 Laction using "`datadir'gc_reduced_B_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if Laction == "10"
	replace sort1 = 5 if Laction == "20"
	replace sort1 = 6 if Laction == "30"
	replace sort1 = 7 if Laction == "90"
	sort sort1
	drop sort1
	order Laction obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd
	save "`datadir'gc_reduced_B_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_fit_B_rowtransit_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_B_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp4_`ext'.dta"

	*********************************
	* Column transition matrix, age 30 *
	*********************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_fit_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	rename t tax_yr
	sort pid_int tax_yr	
	replace age = age+25
	
	keep if age == 30
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab Laction, gen(Laction_shr)
	
	*** calculate the share of action by action ***
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 (count) ind=n_ind obs=pid_int [aw=q]
		gen action = "`act'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`act'" == "10" ///
    	{
    		save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
    	}
  		if "`act'" != "10" ///
    	{
    		append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 (count) ind=n_ind obs=pid_int [aw=q]
	gen action = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 [aw=q]
		gen action = "`act'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 [aw=q]
	gen action = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order action
	sort action
	save "`datadir'gc_reduced_B_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order action
	sort action
	forvalues j = 1(1)4 ///
	{
		rename Laction_shr`j' Laction_shr`j'_sd
	}
	merge 1:1 action using "`datadir'gc_reduced_B_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if action == "10"
	replace sort1 = 5 if action == "20"
	replace sort1 = 6 if action == "30"
	replace sort1 = 7 if action == "90"
	sort sort1
	drop sort1
	order action obs ind ///
	Laction_shr1 Laction_shr1_sd ///
	Laction_shr2 Laction_shr2_sd ///
	Laction_shr3 Laction_shr3_sd ///
	Laction_shr4 Laction_shr4_sd
	save "`datadir'gc_reduced_B_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_fit_B_coltransit_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_B_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp4_`ext'.dta"
	
	******************************
	* choices over the lifecycle *
	******************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_fit_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	rename t tax_yr
	sort pid_int tax_yr	
	replace age = age+25
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by age ***
	forvalues ag = 1(1)10 ///
	{
		preserve
		keep if age-`a' == `ag'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 (count) ind=n_ind obs=pid_int [aw=q]
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`ag'" == "1" ///
    	{
    		save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
    	}
  		if "`ag'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 (count) ind=n_ind obs=pid_int [aw=q]
	gen age = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues ag = 1(1)10 ///
	{
		preserve
		keep if age-`a' == `ag'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 [aw=q]
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 [aw=q]
	gen age = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order age
	sort age
	save "`datadir'gc_reduced_B_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order age
	sort age
	forvalues j = 1(1)4 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 age using "`datadir'gc_reduced_B_tmp3_`ext'.dta", update
	drop _merge
	sort age
	order age obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd
	save "`datadir'gc_reduced_B_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_fit_B_lifecycle_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_B_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp4_`ext'.dta"
	/*
	****************************
	* wages over the lifecycle *
	****************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_fit_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	rename t tax_yr
	sort pid_int tax_yr	
	replace age = age+25
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	drop if action == 90
	
	preserve
	sort pid_int tax_yr
	collapse (mean) ln_y, by(action age)
	tostring(age), replace
	tostring(action), replace
	gen stat_type = "mean"
  	* save it or stack it *
	save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore

	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	collapse (mean) ln_y, by(age)
	gen action = "0"
	tostring(age), replace
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	preserve
	collapse (sd) ln_y, by(action age)
	tostring(age), replace
	tostring(action), replace
	gen stat_type = "sd"
  	* save it or stack it *
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore

	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) ln_y, by(age)
	gen action = "0"
	tostring(age), replace
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_B_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_B_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order action age
	sort action age
	save "`datadir'gc_reduced_B_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_B_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order action age
	sort action age
	rename ln_y ln_y_sd
	merge 1:1 age action using "`datadir'gc_reduced_B_tmp3_`ext'.dta", update
	drop _merge
	sort action age
	order age action ///
	ln_y ln_y_sd
	save "`datadir'gc_reduced_B_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_fit_B_lifecycle_lny_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_B_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_B_tmp4_`ext'.dta"
	*/
	disp "***** STEP 2: fit (COMPLETED) *****"
}
disp "***** Finished processing STEP 2 *****"
endtime

}
}


********************************************
* STEP X: delete all intermediate datasets *
********************************************
disp "***** Started processing STEP X *****"
starttime
if 1 == `gateX' ///
{
	disp "***** STEP X: delete all intermediate datasets *****"
	local datadir T:\${data_folder}\
	
	disp "***** STEP X: delete all intermediate datasets (COMPLETED) *****"
}
disp "***** Finished processing STEP X *****"
endtime

********
* EXIT *
********
clear all
log close
